﻿using Arction.Wpf.BindableCharting.SeriesXY;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF自由布局.Models;
using WPF自由布局.ViewModels;

namespace WPF自由布局.Views
{
    /// <summary>
    /// BodeBindableControl.xaml 的交互逻辑
    /// </summary>
    public partial class BodeBindableControl : UserControl
    {
        public BodeBindableControl()
        {
            InitializeComponent();
            ConfigChart();
        }

        private void ConfigChart()
        {
            chart.ColorTheme = Arction.Wpf.BindableCharting.ColorTheme.LightGray;
            chart.Title.Text = "波特图";
            chart.Title.Color = Colors.DeepSkyBlue;
            chart.Padding = new Thickness(0);
            chart.Margin = new Thickness(0);
            chart.ChartBackground.Color = Colors.Transparent;
            Debug.WriteLine("Chart configuration completed.");
        }


        public static readonly DependencyProperty ItemsSourceProperty =
    DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<SensorViewModel>), typeof(BodeBindableControl),
        new PropertyMetadata(null, OnItemsSourceChanged));

        public ObservableCollection<SensorViewModel> ItemsSource
        {
            get => (ObservableCollection<SensorViewModel>)GetValue(ItemsSourceProperty);
            set => SetValue(ItemsSourceProperty, value);
        }



        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as BodeBindableControl;
            if (control == null) return;

            Debug.WriteLine("ItemsSource changed.");

            if (e.OldValue is ObservableCollection<SensorViewModel> oldCollection)
            {
                oldCollection.CollectionChanged -= control.OnItemsCollectionChanged;
                Debug.WriteLine("Detached CollectionChanged event handler from old ItemsSource.");
            }
            if (e.NewValue is ObservableCollection<SensorViewModel> newCollection)
            {
                newCollection.CollectionChanged += control.OnItemsCollectionChanged;
                Debug.WriteLine("Attached CollectionChanged event handler to new ItemsSource.");
                foreach (var sensor in newCollection)
                {
                    control.AddSeriesForSensor(sensor);
                }
            }


        }

        private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Debug.WriteLine($"ItemsSource collection changed. Action: {e.Action}");

            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                // 为每个新 Sensor 添加一个曲线
                foreach (SensorViewModel newSensor in e.NewItems)
                {
                    AddSeriesForSensor(newSensor);
                }
            }
        }

        private void AddSeriesForSensor(SensorViewModel sensor)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                Debug.WriteLine($"Adding series for sensor: {sensor.SensorName}");


                var pointLineSeries = new PointLineSeries
                {
                    Title = new Arction.Wpf.BindableCharting.Titles.SeriesTitle() { Text = $"{sensor.SensorName}", Visible = true },
                    Points = sensor.DataPoints1, // 这里直接绑定数据源
                    LineStyle = new Arction.Wpf.BindableCharting.LineStyle() { Color = Color.FromArgb(222, 122, 225, 255)}
                };
                viewXY.PointLineSeries.Add(pointLineSeries);
                Debug.WriteLine($"Series added for sensor: {sensor.SensorName}");
            });

            
        }
    }
}
